package com.ciaojian.core.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ciaojian.core.model.WorkerInvite;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 发型师邀请记录mapper
 *
 * @author Atlakyn
 */
@Mapper
public interface WorkerInviteMapper extends BaseMapper<WorkerInvite> {

    /**
     * 用户当前绑定的发型师
     *
     * @param userId 用户 id
     * @return 用户id & 结束时间为null 的数据
     */
    WorkerInvite getByUserIdAndEndTimeIsNull(@Param("userId") Integer userId);

    /**
     * 根据UserId 查询 WorkerId
     *
     * @param userId 用户id
     * @return workerId
     */
    @Select("  SELECT worker_id\n" +
            "        FROM d_worker_invite wi\n" +
            "                     LEFT JOIN d_worker AS w ON w.id = wi.worker_id\n" +
            "        WHERE wi.user_id = #{userId}\n" +
            "          AND wi.end_time IS NULL\n" +
            "          AND w.status != 8\n" +
            "        ORDER BY wi.create_time DESC\n" +
            "        LIMIT 1")
    Integer getWorkerIdByUserId(@Param("userId") Integer userId);
}
